package com.easyoffer.controller;

import com.easyoffer.annotation.RoleCheck;
import com.easyoffer.exception.BusinessException;
import com.easyoffer.model.dto.question.QuestionBatchAddRequest;
import com.easyoffer.model.dto.question.QuestionBatchRemoveRequest;
import com.easyoffer.service.QuestionBankQuestionService;
import com.easyoffer.common.BaseResponse;
import com.easyoffer.common.DeleteRequest;
import com.easyoffer.common.ErrorCode;
import com.easyoffer.common.util.ResultUtils;
import com.easyoffer.model.dto.questionbankquestion.QuestionBankQuestionAddRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/questionBankQuestion")
public class QuestionBankQuestionController {
    @Autowired
    QuestionBankQuestionService questionBankQuestionService;

    @PostMapping("/add")
    @RoleCheck(mustRole = "admin")
    public BaseResponse<Boolean> addQuestionBankQuestion(@RequestBody QuestionBankQuestionAddRequest addRequest, HttpServletRequest request) {
        if (addRequest == null)
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        if (addRequest.getQuestionId() == null || addRequest.getQuestionBankId() == null)
            return  ResultUtils.error(ErrorCode.PARAMS_ERROR);
        Boolean added = questionBankQuestionService.addQuestionBankQuestion(addRequest,request);
        return ResultUtils.success(added);
    }

    @PostMapping("/delete")
    @RoleCheck(mustRole = "admin")
    public BaseResponse<Boolean> deleteQuestionBankQuestion(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null)
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        if (deleteRequest.getId() == null)
            return  ResultUtils.error(ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(questionBankQuestionService.deleteQuestionBankQuestion(deleteRequest.getId()));
    }

    @PostMapping("/batch/remove")
    public BaseResponse<Boolean> batchRemoveQuestionToBank(@RequestBody QuestionBatchRemoveRequest batchRemoveRequest, HttpServletRequest request){
        if (batchRemoveRequest == null)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        Boolean added = questionBankQuestionService.batchRemoveQuestionFromBank(batchRemoveRequest.getQuestionBankId(), batchRemoveRequest.getQuestionIdList(),request);
        return ResultUtils.success(added);
    }

    @PostMapping("/batch/add")
    public BaseResponse<Boolean> batchAddQuestionToBank(@RequestBody QuestionBatchAddRequest batchAddRequest, HttpServletRequest request){
        if (batchAddRequest == null)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        Boolean added = questionBankQuestionService.saveBatchToBank(batchAddRequest.getQuestionBankId(), batchAddRequest.getQuestionIdList(),request);
        return ResultUtils.success(added);
    }
}
